package com.ece.devirtualizus.Dao;

import java.io.File;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SqlLiteSessionStoreHelper extends SQLiteOpenHelper {

	Context context;
	private static final String DATABASE_NAME = "sessionstore1.db";
	private static final int DATABASE_VERSION = 1;

	public static final String TABLE_MODULE_TYPE = "ModuleType";
	public static final String COLUMN_MODULE_TYPE_ID = "id";
	public static final String COLUMN_MODULE_TYPE_LIBELLE = "libelle";

	public static final String TABLE_ACTION_RELEVE = "ActionReleve";
	public static final String COLUMN_ACTION_RELEVE_ID = "id";
	public static final String COLUMN_ACTION_RELEVE_NB_REPET = "nbRepetitions";
	public static final String COLUMN_ACTION_RELEVE_ID_TYPE = "idType";
	public static final String COLUMN_ACTION_RELEVE_POINTS_SESSION = "pointsSession";
	public static final String COLUMN_ACTION_RELEVE_DATE_BEGIN = "dateBeginSession";
	public static final String COLUMN_ACTION_RELEVE_DATE_END = "dateEndSession";

	private static final String DATABASE_CREATE_TABLE_MODULE_TYPE = "create table IF NOT EXISTS "
			+ TABLE_MODULE_TYPE
			+ "("
			+ COLUMN_MODULE_TYPE_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_MODULE_TYPE_LIBELLE + " TEXT not null);";

	private static final String DATABASE_CREATE_TABLE_ACTION_RELEVE = "create table IF NOT EXISTS "
			+ TABLE_ACTION_RELEVE
			+ "("
			+ COLUMN_ACTION_RELEVE_ID
			+ " integer primary key autoincrement, "
			+ COLUMN_ACTION_RELEVE_NB_REPET
			+ " integer not null, "
			+ COLUMN_ACTION_RELEVE_ID_TYPE
			+ " integer not null, "
			+ COLUMN_ACTION_RELEVE_POINTS_SESSION
			+ " integer not null, "
			+ COLUMN_ACTION_RELEVE_DATE_BEGIN
			+ " TEXT not null, "
			+ COLUMN_ACTION_RELEVE_DATE_END + " TEXT not null);";

	public SqlLiteSessionStoreHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE_TABLE_MODULE_TYPE);
		Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_MODULE_TYPE);

		db.execSQL(DATABASE_CREATE_TABLE_ACTION_RELEVE);
		Log.i("DATABASE_CREATE", DATABASE_CREATE_TABLE_ACTION_RELEVE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_TABLE_MODULE_TYPE + ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS "
				+ DATABASE_CREATE_TABLE_MODULE_TYPE + ";");

		db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_TABLE_ACTION_RELEVE + ";");
		Log.i("onUpgrade", "DROP TABLE IF EXISTS " + DATABASE_CREATE_TABLE_ACTION_RELEVE
				+ ";");
		
		onCreate(db);
	}

	public void destroyDatabase(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_TABLE_MODULE_TYPE + ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS "
				+ DATABASE_CREATE_TABLE_MODULE_TYPE + ";");

		db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_TABLE_ACTION_RELEVE + ";");
		Log.i("onDestroy", "DROP TABLE IF EXISTS " + DATABASE_CREATE_TABLE_ACTION_RELEVE
				+ ";");
	}
	
	public boolean checkDataBase() {
		File dbFile = context.getDatabasePath(context.getFilesDir().getPath()
				+ "/../databases/sessionstore1.db");
		// .getDatabasePath("/data/data/com.ece.devirtualizus/databases/gpsposition1.db");
		// context.getDatabasePath(context.getFilesDir().getPath()+"/data/com.ece.devirtualizus/databases/progressbar1.db");
		return dbFile.exists();
		// Log.d("test path",
		// getDatabasePath(getFilesDir().getPath()+"/../databases/gpsposition1.db").toString());
		// return false;
	}

}
